<template>
  <!-- <uni-page-head>
    <uni-nav-bar title="重置密码" left-icon="back" @click-left="back"></uni-nav-bar>
  </uni-page-head> -->

  <view class="content">
    <uni-forms ref="resetPasswordForm" :rules="rules" :model="formData" validate-trigger="blur">
      <uni-forms-item label="原密码" name="oldPassword">
        <uni-easyinput v-model="formData.oldPassword" type="password" placeholder="请输入原密码" />
      </uni-forms-item>
      <uni-forms-item label="新密码" name="newPassword">
        <uni-easyinput v-model="formData.newPassword" type="password" placeholder="请输入新密码" />
      </uni-forms-item>
      <uni-forms-item label="确认新密码" name="confirmPassword">
        <uni-easyinput v-model="formData.confirmPassword" type="password" placeholder="请再次输入新密码" />
      </uni-forms-item>

      <view class="button-container">
        <button type="primary" @click="submit">
          提交
        </button>
      </view>
    </uni-forms>
  </view>
</template>

<script setup>
  import {
    ref,
    getCurrentInstance
  } from 'vue'

  const formData = ref({
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
  })

  const rules = {
    oldPassword: {
      rules: [{
        required: true,
        errorMessage: '原密码不能为空'
      }],
      label: '原密码'
    },
    newPassword: {
      rules: [{
          required: true,
          errorMessage: '新密码不能为空'
        },
        {
          minLength: 6,
          maxLength: 20,
          errorMessage: '新密码长度应在6到20个字符之间'
        }
      ],
      label: '新密码'
    },
    confirmPassword: {
      rules: [{
          required: true,
          errorMessage: '确认密码不能为空'
        },
        {
          validateFunction: function(rule, value, data, callback) {
            console.log("自定义校验");
            console.log(data);
            if (value !== data.newPassword) {
              callback('两次输入的密码不一致')
            } else {
              return true
            }
          }
        }
      ],
      label: '确认新密码'
    }

  }

  function validateConfirmPassword(rule, value, data, callback) {
    console.log("自定义校验");
    console.log(data);
    if (value !== data.newPassword) {
      callback(new Error('两次输入的密码不一致'))
    } else {
      return true
    }
  }

  const {
    proxy
  } = getCurrentInstance()

  function submit() {
    console.log(formData.value.oldPassword);
    proxy.$refs.resetPasswordForm.validate().then(res => {
      console.log('表单数据信息：', res)
      // 修改密码
      http.request({
        url: '/user/updatePassword',
        method: 'PUT',
        data: {
          ...res
        }
      }).then((res)=>{
        console.log(res);
        if (res.code == 1) {
          uni.showToast({
            title: '修改成功',
            icon: 'none'
          })
          uni.reLaunch({
            url: '/pages/index/index',
          })
          uni.clearStorageSync()
          }else{
            uni.showToast({
              title: res.msg,
              icon: 'none'
            })
          }

      })

    }).catch(err => {
      console.log('表单错误信息：', err)
    })

  }

  // function back() {
  //   // 返回上一页逻辑，如使用 uni.navigateBack()
  //   console.log('返回上一页');
  // }
</script>

<style scoped>
  .content {
    padding: 30px;
  }

  .button-container {
    margin-top: 20px;
  }
</style>
